﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Util
{
    public class WebRequesUtil
    {
        public static bool HttpRequest(string Url, string Params, string Method, Dictionary<string, string> list, out string Result, out string Exception)
        {
            Result = null;
            Exception = null;

            if (Method.ToLower() == "get")
                return HttpGet(Url, Params, out Result, out Exception);
            else
                return HttpPost(Url, Params, list, out Result, out Exception);
        }

        private static bool HttpGet(string Url, string Params, out string Result, out string Exception)
        {
            Result = null;
            Exception = null;
            try
            {
                byte[] data = Encoding.UTF8.GetBytes(Params);
                WebRequest request = (WebRequest)HttpWebRequest.Create(Url + "?" + Params);
                request.Method = "GET";
                //request.ContentType = "application/x-www-form-urlencoded";
                //request.ContentLength = data.Length;
                request.Timeout = 5000;
                using (WebResponse response = request.GetResponse())
                {
                    if (null == response)
                        return false;
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            Result = reader.ReadToEnd();
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Exception = ex.Message;
                return false;
            }
        }

        private static bool HttpPost(string Url, string Params, Dictionary<string, string> list, out string Result, out string Exception)
        {
            Result = null;
            Exception = null;
            try
            {
                byte[] data = Encoding.UTF8.GetBytes(Params);
                HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
                request.Timeout = 3000000;
                request.ReadWriteTimeout = 3000000;
                request.ContinueTimeout = 3000000;
                request.Method = "POST";
                int ContentLength = 0;
                request.ContentType = "application/x-www-form-urlencoded";
                ContentLength = data.Length;
                request.ContentLength = ContentLength;
                request.Timeout = 5000;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, ContentLength);
                }
                if (list != null && list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        request.Headers.Add(item.Key, item.Value);

                    }
                }
                using (WebResponse response = request.GetResponse())
                {
                    if (null == response)
                        return false;
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            Result = reader.ReadToEnd();
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Exception = ex.Message;
                return false;
            }
        }
    }
}
